package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.R;
import com.qire.manhua.activity.HomeActivity;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.FragmentBookshelfHistoryBinding;
import com.qire.manhua.databinding.LayoutHomeBookshelfHistoryItemBinding;
import com.qire.manhua.fragment.BookshelfHistoryFragment;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.BookshelfHistoryViewModel;
import com.qire.manhua.model.viewModel.impl.BookshelfHistoryViewModel.HistoryItemViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;

public class BookshelfHistoryController extends EventController<BookshelfHistoryFragment> implements AutoVariable<LayoutHomeBookshelfHistoryItemBinding> {

    private BookshelfHistoryViewModel viewModel;

    public BookshelfHistoryController(@NonNull BookshelfHistoryFragment owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        FragmentBookshelfHistoryBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setBookshelfHistoryController(this);
        }
    }

    @Override
    public void bindVariable(LayoutHomeBookshelfHistoryItemBinding viewBind) {
        viewBind.setBookshelfHistoryController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BookshelfHistoryViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**********************************************************************************************/

//    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
//    }
    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadHistoryList();
    }

    public void delBtnOnClick(View view) {

        int batchDeleteCount = viewModel.batchDeleteCount();

        if(batchDeleteCount < 1){
            return;
        }

        DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_l)
                .setTextView(R.id.dialog_title, "共选中" + batchDeleteCount + "个作品")
                .setTextView(R.id.dialog_content, "是否删除已选中作品?")
                .setTextView(R.id.dialog_button_light, "取消", cancelView -> DialogFastBuildUtil.dismissDialogBy(cancelView))
                .setTextView(R.id.dialog_button_dark, "确定", confirmView -> {
                    viewModel.submitDelHistory();
                    DialogFastBuildUtil.dismissDialogBy(confirmView);
                })
                .show();

    }

    public void defDataEmptyBtnOnClick(View view) {
        if(!LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            if(viewModel.isException()) {
                viewModel.loadHistoryList();
            } else {
                HomeActivity homeActivity = ObjectUtil.as(owner.getActivity(), HomeActivity.class);
                if(ObjectUtil.nonNull(homeActivity)) {
                    homeActivity.gotoTabIndex(PageTabIndex.TAB_INDEX_HOME_RECOMMEND, PageTabIndex.TAB_INDEX_INVALID);
                }
            }
        }
    }

    public void historyItemOnClick(View view) {

        HistoryItemViewModel historyItemViewModel = (HistoryItemViewModel) view.getTag();

        if(viewModel.isEditing()){
            historyItemViewModel.toggle();
        } else {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                    .withData("comicBookEntity", historyItemViewModel.comicBookEntity)
                    .navigation();
        }
    }

    public void historyItemReadOnClick(View view) {
        HistoryItemViewModel historyItemViewModel = (HistoryItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                .withData("comicBookEntity", historyItemViewModel.comicBookEntity)
                .navigation();
    }

}
